package com.fitsoftwareprocess.mobilemap.bus;

import java.util.ArrayList;
import java.util.List;

import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import android.util.Log;

import com.fitsoftwareprocess.mobimap.dto.Coordinate;
import com.fitsoftwareprocess.mobimap.dto.PlaceInfo;
import com.fitsoftwareprocess.mobimap.dto.PlaceTypeInfo;

public class ServiceFinder {
	private String _Url;
	private MyWebClient _MyWebClient;

	public ServiceFinder() {
		_MyWebClient = new MyWebClient("http://tempuri.org/");
		_Url = "http://10.0.2.2:1609/Service1.asmx";
	}
	
	public List<PlaceInfo> GetListService(String keyword)
	{
		List<PlaceInfo> list = new ArrayList<PlaceInfo>();
		String methodName = "GetListService";
		SoapObject req = _MyWebClient.GetSoapObject(methodName);
		req.addProperty("keyword",keyword);
		SoapSerializationEnvelope envelope = _MyWebClient.GetEnvelope(req);
		HttpTransportSE androidHttpTransport = new HttpTransportSE(_Url);
        androidHttpTransport.debug = true;

        try 
        {
        	//SoapPrimitive soapResult = _MyWebClient.MakeCall(_Url, envelope, _MyWebClient.getNameSpace(), methodName);
            androidHttpTransport.call("http://tempuri.org/" + methodName, envelope);
            SoapObject response = (SoapObject) envelope.getResponse();
            final int intPropertyCount = response.getPropertyCount();
            for (int i = 0; i < intPropertyCount; i++) 
            {
                SoapObject responseChild = (SoapObject) response.getProperty(i);
                PlaceInfo placeInfo = new PlaceInfo();
                if (responseChild.hasProperty("FullName")) {
                    placeInfo.set_fullName(responseChild.getProperty("FullName").toString());
                }
                if (responseChild.hasProperty("Address")) {
                    placeInfo.set_address(responseChild.getProperty("Address").toString());
                }
                if (responseChild.hasProperty("ID")) {
                    placeInfo.set_id(Integer.parseInt(responseChild.getProperty("ID").toString()));
                }
               
                
                if (responseChild.hasProperty("Type")) {
                	SoapObject temp =(SoapObject) responseChild.getProperty("Type");
                	PlaceTypeInfo placeTypeInfo = new PlaceTypeInfo();
                	SoapPrimitive id = (SoapPrimitive)temp.getProperty(0);
                	SoapPrimitive description = (SoapPrimitive)temp.getProperty(1);
                	SoapPrimitive piture = (SoapPrimitive)temp.getProperty(2);
                	placeTypeInfo.set_id(Integer.parseInt(id.toString()));
                	placeTypeInfo.set_description(description.toString());
                	placeTypeInfo.set_picture(piture.toString());
           
                	placeInfo.set_type(placeTypeInfo);
                }
                if (responseChild.hasProperty("Location")) {
                	SoapObject temp =(SoapObject) responseChild.getProperty("Location");
                	Coordinate coordinate = new Coordinate();
                	SoapPrimitive longitude = (SoapPrimitive)temp.getProperty(0);
                	SoapPrimitive latitude = (SoapPrimitive)temp.getProperty(1);
                	coordinate.set_longitude(Float.parseFloat(longitude.toString()));
                	coordinate.set_latitude(Float.parseFloat(latitude.toString()));
           
                	placeInfo.set_location(coordinate);
                }
                if (responseChild.hasProperty("Info")) {
                	
                	placeInfo.set_info(responseChild.getProperty("Info").toString());
                }
                if (responseChild.hasProperty("Picture")) {
                	placeInfo.set_picture(responseChild.getProperty("Picture").toString());
                }
                if (responseChild.hasProperty("Website")) {
                	placeInfo.set_website(responseChild.getProperty("Website").toString());
                }
                if (responseChild.hasProperty("Phone")) {
                	placeInfo.set_phone(responseChild.getProperty("Phone").toString());
                }
                list.add(placeInfo);
            }
        }catch (Exception e) {
            Log.d("aa", e.getMessage());

            /** if an error handled arrPhongTro setting null */
            list = null;
        }

		return list;
	}
}
